import store from '@/store'
import { getPublicConfig, getUserInfo } from '@/api/user'

export default {
  namespaced: true,
  state: {
    publicConfig: {},
  },
  actions: {
    init({ state }, { userId }) {
      return new Promise(async (resolve, reject) => {
        resolve()
      });
    },
    initPublicConfig({ state, dispatch }, {}) {
      return new Promise(async resolve => {
        const response = await getPublicConfig({})
        
        const pubConfig = response.pubConfig || {}
        state.publicConfig = pubConfig

        const userInfo = await dispatch('admin/db/get', {
          dbName: 'sys',
          path: 'user.info',
          defaultValue: {},
          user: true
        }, { root: true });
        store.commit('admin/layout/updateWaterMarkText', `${userInfo.name || ''}`)
        await dispatch('admin/db/set', {
          dbName: 'config',
          path: 'pub',
          value: pubConfig,
        }, { root: true });
        resolve();
      });
    },
    initUserInfo({ dispatch }) {
      return new Promise(async resolve => {
        const response = await getUserInfo({})
        await dispatch('admin/user/set', response, { root: true });
        resolve()
      });
    }
  }
}
